var ws = "ws://81.68.88.88:8092/web";
//var ws = "ws://127.0.0.1:8092/web";
//全局变量配置
window.tk="";
window.uid="";
window.模式="个人中心";
window.actions="请求gpt"
var selectElements = document.querySelectorAll('select');
selectElements.forEach(function(selectElement) {
  selectElement.addEventListener('change', function() {
    var selectedOption = this.options[this.selectedIndex];
    var selectedValue = selectedOption.value;
    console.log(selectedValue);
  });
});


function du(txt){

    /*var synth = window.speechSynthesis;
var utterance = new SpeechSynthesisUtterance('Hello, world!');
synth.speak(utterance);*/
/*
var synth = window.speechSynthesis;
var utterance = new SpeechSynthesisUtterance(txt);
utterance.lang = 'zh-CN';
utterance.voice = synth.getVoices().find(function(voice) {
return voice.name === 'Microsoft Huihui Desktop - Chinese (Simplified)';
});
synth.speak(utterance);
*/
var synth = window.speechSynthesis;
var utterance = new SpeechSynthesisUtterance(txt);
utterance.lang = 'en-US';
utterance.voice = synth.getVoices().find(function(voice) {
    return voice.name === 'Google US English';
});
synth.speak(utterance);
console.log("说话完毕");
}
// 连接WebSocket
function connectWebSocket() {
if(ws == null || ws.readyState != WebSocket.OPEN) {
if(window.WebSocket) {
//var wsPath = document.getElementById('urlId').value;
var wsPath=ws;
if(wsPath != null && wsPath != '') {
    ws = new WebSocket(wsPath);
    ws.onopen = function(event) {
        console.log("websocket连接成功..." + wsPath);
        creatdiv("WebSocket 连接成功", "");
    };
    //接受到消息处理
    ws.onmessage = function(event) {
        //console.log("websocket接收数据：" + event.data);
        //du(event.data);
        //creatdiv(event.data);
        //接受到的消息
        if(!event.data){
            return;
        }
        var json;
        try{
            json=JSON.parse(event.data);
        }
        catch(e){
            return;
        }
        var data=json;
        if(data.evals){
            eval(data.evals);
        }
        if(data.uid!=null&&data.tk!=null){
            window.tk=data.tk;
            window.uid=data.uid;
        }
        console.log("当前uid"+data.uid+"当前token"+data.tk);
        if(data.action=="login"&&(data.msg=="登录成功"||data.msg=="注册成功")){
          $("content").style.display="none";
          $("container").style.display="flex";
          console.log(data.uid+"前uid后tk"+data.tk);
          console.log(data.action);
          creatdiv(data.msg, "服务端 ");
        }else if(data.msg=="密码错误!!!!!!"){
            alert("密码错误!!!!!!");
            console.log(data.action);
        }
        //指定对话模式
        //初始请求
        //{"action":"口语对话模式","uid":"",tk:""}//初始请求

        //{"action":"口语对话","assistant":"","user":""}

        //{"action":"口语对话并请求评价","userReply":"","uid":"",tk:""}

        //{"action":"口语评价","content":"","score":""}

        //口语对话模式
        //练习对话模式
        //指定对话模式
        //初始请求
        //{"action":"练习对话模式","uid":"",tk:""}//初始请求

        //{"action":"练习对话","assistant":"","user":""}

        //{"action":"练习对话并请求评价","userReply":"","uid":"",tk:""}

        //{"action":"练习评价","content":"","score":""}


        if(data.action=="回复"){
            du(data.txt);
            creatdiv(data.txt, "服务端 ");
        }
        if(data.action=="alert"){
           alert(data.txt);
        }
        if(data.action=="msg"){
            //du(data.txt)
            if(data.txt=="当前文章不存在"){
                window.shunxu=0;
                creatdiv("当前对话结束", "服务端 ");
            }else
            creatdiv(data.txt, "服务端 ");
        }
    
        if(data.action=="文章目录"){
            
            //du(data.txt)
            //window.shunxu=0;
            createSelect(data.txt);
            //creatdiv(data.txt+"这是目录信息", "服务端 ");
        }
        //creatdiv(event.data, "服务端 ");
       
    };
    ws.onclose = function(event) {
        console.log("websocket关闭..." + wsPath);
        creatdiv("WebSocket 关闭", "");
    };
    ws.onerror = function(event) {
        console.log("websocket异常..." + wsPath);
        creatdiv("WebSocket 异常", "");
    };
} else {
    alert("请输入正确的websocket地址");
}
} else {
alert("您的浏览器不支持WebSocket协议！");
}
} else {
creatdiv("WebSocket 已连接", "");
}
}



//关闭WebSocket连接
function closeWebSocket() {
    if(ws != null && ws.readyState == WebSocket.OPEN){
    ws.close();
    } else {
    console.log("websocket已断开...");
    creatdiv("WebSocket 已断开", "");
    }
}



function ws_send(data)
{
data.uid=window.uid
data.tk=window.tk
data.版本="1.0"
data.模式=window.模式
if(data.action=="请求gpt"){
    creatdiv(data.text,"我");
}else if(data.action=="情景模式对话中"||data.action=="自由对话"||data.action=="模拟面试对话"){
    creatdiv(data.userTxt,"我");
}
data=JSON.stringify(data)
ws.send(data)

}

//发送消息
function sendMessage(msg) {
    if(ws != null && ws.readyState == WebSocket.OPEN){
    //var msg = document.getElementById('msgId').value;
    //creatdiv(msg,"我");
    ws_send(msg);
    } else {
    creatdiv("WebSocket 已断开", "");
    }
}
/**
 * 表示在id="msg-div"的盒子新增一个div
 */
//  creatdiv("WebSocket 已断开", "");
function $(id){
    return document.getElementById(id);
 }

 function displaydiv(){
       // 获取所有类名为"dropdown"的元素，返回一个HTMLCollection对象
var dropdowns = document.getElementsByClassName("dropdown");

// 遍历HTMLCollection对象中的所有元素，并将它们的visibility属性设置为"visible"
for(var i = 0; i < dropdowns.length; i++) {
  dropdowns[i].style.visibility = "visible";
}
 }
function hidediv(){
    // 获取所有类名为"dropdown"的元素，返回一个HTMLCollection对象
var dropdowns = document.getElementsByClassName("dropdown");

// 遍历HTMLCollection对象中的所有元素，并将它们的visibility属性设置为"visible"
for(var i = 0; i < dropdowns.length; i++) {
dropdowns[i].style.visibility = "hidden";
}
 }
 $("moshi").innerHTML=window.模式;
 
 function removeClass(){
    $("a").classList.remove('btn-small');
    $("b").classList.remove('btn-small');
    $("c").classList.remove('btn-small');
    $("d").classList.remove('btn-small');
    $("moshi").innerHTML=window.模式;
    hidediv();
    clearMessage("msg-div");
 }

 $("a").onclick=function(){
     window.模式="自由模式";
     actions="自由对话"
     removeClass();
     $("a").classList.add('btn-small');
 }
 $("b").onclick=function(){
    window.模式="情景模式";
    actions="情景模式对话中";
    var qj={
        action:"请求情景分类"
    };
    removeClass();
    displaydiv();
    ws_send(qj,"我")
    $("b").classList.add('btn-small');
}
$("c").onclick=function(){
    window.模式="模拟训练";
    actions="模拟面试对话";
    var qj={
        action:"初始化面试对话"
    };
    removeClass();
    displaydiv();
    ws_send(qj,"我")
    removeClass();
    $("c").classList.add('btn-small');
    //createSelect("");
}
$("d").onclick=function(){
    window.模式="用户中心";
    removeClass();
    createyUserCenter();
    $("d").classList.add('btn-small');
}

$("send").onclick=function(){
    var res;
    txts=$("inputTxt").value;
    if(actions=="情景模式对话中"){
        txts="文章"+"-"+window.选择文章+"-"+window.shunxu;
        window.shunxu++;
    }
    res={
      userTxt:$("inputTxt").value,
      text: txts,
      action:actions
    };
    console.log(txts);
    data=JSON.stringify(res);
    //creatdiv(txts,"我");
    ws_send(res);
}





/*class函数
//添加Class
document.getElementById('head').classList.add('add') //('add1','add2')
//<div id="head" class="add"></div>

//删除Class
document.getElementById('head').classList.remove('add')
//<div id="head"></div>


//切换class toggle
document.getElementById('head').classList.toggle('add')
//打印true 代表添加成功，<div id="head" class="add"></div>
//打印false为删除成功, <div id="head"></div>

//是否存在Class
document.getElementById('head').classList.contains('add')
//返回 true和false

//返回类名在元素中的索引值。索引值从 0 开始。
<div id="head" class="add remove"></div>
document.getElementById('head').classList.item(1)
//remove
 */

